<!-- 中标结果公示 -->
<template>
    <div class="app-container">
        <el-button @click="setPublicTime" type="primary" size="mini" v-if="csTender.pStartTime==null">设置公示时间</el-button>
        <div style="margin-bottom: 20px;"></div>
        <el-table v-loading="loading" :data="candidateList" :default-sort="{ prop: 'ranking', order: 'ascending' }">
            <el-table-column label="序号" type="index" align="center" />
            <el-table-column label="序号" type="index" prop="hId" align="center" v-if="false" />
            <el-table-column label="供应商名称" align="center" prop="hName" />
            <el-table-column label="最终报价(万元)" align="center" prop="finalOffer">
                <template slot-scope="scope">
                    {{ scope.row.finalOffer / 10000 }}
                </template>
            </el-table-column>
            <el-table-column label="最终得分" align="center" prop="finalScore" sortable />
            <el-table-column label="是否推荐" align="center" prop="recommend">
                <template slot-scope="scope">
                    <span>{{ scope.row.recommend == 0 ? '是' : '否' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="排名" align="center" prop="ranking" sortable />
            <el-table-column label="是否中标" align="center" prop="bidder">
                <template slot-scope="scope">
                    <span>{{ scope.row.bidder == 0 ? '是' : '否' }}</span>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="设置公示时间" :visible.sync="candidateOpen" width="60%">
            <el-row :gutter="15">
                <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
                    <el-col :span="12">
                        <el-form-item label="公示开始时间" prop="pStartTime">
                            <el-date-picker type="datetime" v-model="formData.pStartTime" format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss" :style="{ width: '100%' }" placeholder="请选择日期选择"
                                clearable>
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="公示结束时间" prop="pEndTime">
                            <el-date-picker type="datetime" v-model="formData.pEndTime" format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss" :style="{ width: '100%' }" placeholder="请选择日期选择"
                                clearable>
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div slot="footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="handleConfirm">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getBidWinningCandidate, setStartTenderTime } from "@/api/system/purchase.js";
export default {
    data() {
        return {
            candidateOpen: false,
            queryParams: {
                iNumber: '',
            },
            csTender:{
            },//用于判断是已设置公示期
            formData: {
                iNumber: '',
                pStartTime: '',
                pEndTime: ''
            },
            total: {},//用判断是否已存在,公示开始时间
            candidateList: [],
            loading: true,
            rules: {
                pStartTime: [
                    { required: true, message: '请选择公示开始时间', trigger: 'change' }
                ],
                pEndTime: [
                    { required: true, message: '请选择公示结束时间', trigger: 'change' }
                ]
            },
        }
    },
    methods: {
        deepCopy(obj) {
            if (obj === null || typeof obj !== 'object') {
                return obj;
            }

            if (Array.isArray(obj)) {
                return obj.map(item => this.deepCopy(item));
            }

            const copy = {};
            for (const key in obj) {
                if (obj.hasOwnProperty(key)) {
                    copy[key] = this.deepCopy(obj[key]);
                }
            }
            return copy;
        },
        getList() {
            getBidWinningCandidate(this.queryParams.iNumber).then(res => {   
                this.loading = false;                
                if (res.data.csTender != null) {
                    this.candidateList = res.data.candidate.filter(item => item.bidder != 1);
                    this.total = this.deepCopy(res.data);
                    this.formData = this.deepCopy(res.data.csTender);
                    this.csTender=res.data.csTender;
                }
            })
        },
        setPublicTime() {
            this.candidateOpen = true;
        },
        handleConfirm() {
            if (this.total.csTender.pStartTime != null && this.total.csTender.pEndTime != null) {
                this.$message({
                    type: 'warning',
                    message: '不能修改公示时间!'
                });
                return;
            }
            this.formData.iNumber = this.queryParams.iNumber;
            setStartTenderTime(this.formData).then(res => {
                if (res.data > 0) {
                    this.$message({
                        type: 'success',
                        message: '设置成功!'
                    });
                    this.candidateOpen = false;
                    this.csTender.pStartTime=this.formData.pStartTime
                    this.formData = {
                        iNumber: '',
                    }
                }
            })
        },
        close() {

        },
    },
    mounted() {
        this.queryParams.iNumber = this.$route.query.iNumber;
        this.getList();
    },

}
</script>
<style scoped></style>